package com.feng.test.ppt;


import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.poi.hslf.usermodel.*;

/**
 * @author HuangSuip
 */
public class NewPPTAnalyse {
    // XSLFGraphicFrame(图表) XSLFConnectorShape（形状）XSLFGraphicFrame（组织  公式）
    //XSLFPictureShape （图片） XSLFTextBox（文本 继承autoShape） XSLFTable（表格）
    //XSLFAutoShape（标题
    public static void main(String[] args) {
        try (InputStream inputStream = new FileInputStream(new File("E:\\ppt\\3.ppt"))) {
            HSLFSlideShow slideShow = new HSLFSlideShow(inputStream);
            Dimension dimension = slideShow.getPageSize();
            List<HSLFSlideMaster> masterList = slideShow.getSlideMasters();

            List<HSLFSlide> slideList = slideShow.getSlides();
            int i = 1;
            for (HSLFSlide slide : slideList) {
                System.out.println("++++++++++第" + i++ + "张+++++++");
                backgroundAnalyse(slide);

                List<HSLFShape> shapeList = slide.getShapes();
                for (HSLFShape shape : shapeList) {
                    System.out.println("Shape Name: " + shape.getShapeName());
                    System.out.println("Shape Type: " + shape.getShapeType());
                    System.out.println("Shape Class: " + shape.getClass());
                    outputRectangle(shape);
/*                    Object o1 = shapeList.get(0);
                    Object o2 = shapeList.get(1);*/
                    if (shape instanceof HSLFTextShape) {
                        //textShape(shape);
                    }
                    if (shape instanceof HSLFConnectorShape) {
                        connectorShape((HSLFConnectorShape) shape);
                    }
                    if (shape instanceof HSLFTable) {
                        //tableAnalyse((HSLFTable) shape);
                        //tableProps((HSLFTable) shape);
                    }
                    if (shape instanceof HSLFLine) {
                        //lineAnalyse((HSLFLine) shape);
                    }
                    if (shape instanceof HSLFGroupShape) {
                        //groupShapeAnalyse((HSLFGroupShape) shape);
                        //drawShape(shape);
                    }
                    if (shape instanceof HSLFFreeformShape) {
                        //freeformAnalyse((HSLFFreeformShape) shape);
                        //drawShape(shape);
                    }
                    if (shape instanceof HSLFAutoShape) {
                        autoShape((HSLFAutoShape) shape);
                        //drawShape(shape);
                    }
                    if (shape instanceof HSLFPictureShape) {
                        //pictureShape((HSLFPictureShape) shape);
                    }
                    System.out.println();
                }
                //drawSlide(slide);
                //master(dimension, slide);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void outputRectangle(HSLFShape shape) {
        Rectangle2D rectangle2D = shape.getAnchor();
        System.out.println("MinX: " + rectangle2D.getMinX());
        System.out.println("MinY: " + rectangle2D.getMinY());
        System.out.println("Rectangle2D X: " + rectangle2D.getX());
        System.out.println("Rectangle2D Y: " + rectangle2D.getY());
        System.out.println("CenterX: " + rectangle2D.getCenterX());
        System.out.println("CenterY: " + rectangle2D.getCenterY());
        System.out.println("Max X: " + rectangle2D.getMaxX());
        System.out.println("Max Y: " + rectangle2D.getMaxY());
        System.out.println("Rectangle2D H: " + rectangle2D.getHeight());
        System.out.println("Rectangle2D W:  " + rectangle2D.getWidth());
    }

    private static void backgroundAnalyse(HSLFSlide slide) {
        HSLFMasterSheet sheet = slide.getMasterSheet();
        HSLFBackground background = sheet.getBackground();
        HSLFFill fill = background.getFill();
        Color color = fill.getForegroundColor();
        System.out.println("Sheet Color: " + color.toString());
        HSLFBackground background1 = slide.getBackground();
        HSLFFill fill1 = background1.getFill();
        System.out.println("Slide Color: " + fill1.getForegroundColor());
    }

    private static void groupShapeAnalyse(HSLFGroupShape shape) {
        HSLFGroupShape groupShape = shape;
        List<HSLFShape> shapeList1 = groupShape.getShapes();
        for (HSLFShape hslfShape : shapeList1) {
            System.out.println(hslfShape.getClass());
        }
    }

    private static void connectorShape(HSLFConnectorShape shape) {

    }

    private static void pictureShape(HSLFPictureShape shape) {
        System.out.println(shape);
    }

    private static void autoShape(HSLFAutoShape shape) {
        Rectangle2D anchor = shape.getAnchor();
        System.out.println("Rotation: " + shape.getRotation());
        System.out.println("LineWidth: " + shape.getLineWidth());
        System.out.println("D_X: " + (anchor.getMaxX() - anchor.getMinX()));
        //复合类型 ()
        System.out.println("Compound: " + shape.getLineCompound());
        //线条类型 轮廓 (短划线类型)
        System.out.println("Outline: " + shape.getLineDash());
        //末端类型(线端类型)
        System.out.println("End cap style: " + shape.getLineCap());
        //线条属性 包括头 尾形状 大小
        System.out.println("Decoration: " + shape.getLineDecoration());
        //System.out.println(shape.getXmlObject());
        System.out.println("Shadow: " + shape.getShadow());
        System.out.println("FlipVertical: " + shape.getFlipVertical());
        System.out.println("FlipHorizontal: " + shape.getFlipHorizontal());
        System.out.println("LineHeadLength: " + shape.getLineHeadLength());
        System.out.println("LineTailLength: " + shape.getLineTailLength());
        System.out.println(shape.getSpContainer());
    }


    private static void drawShape(HSLFShape shape, String... name) {
        Rectangle2D rectangle2D = shape.getAnchor();
        BufferedImage image = new BufferedImage((int) rectangle2D.getWidth() + 1, (int) rectangle2D.getHeight() + 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.fill(new Rectangle2D.Double(0, 0, rectangle2D.getWidth(), rectangle2D.getHeight()));
        //图片旋转
        Rectangle2D rectangle2D1 = new Rectangle2D.Double(0, 0, rectangle2D.getWidth(), rectangle2D.getHeight());
        shape.draw(graphics2D, rectangle2D1);
        String fileName = null;
        if (name.length > 0) {
            fileName = name[0];
        }
        try (FileOutputStream fos = new FileOutputStream(new File("E:\\ppt\\slide\\" + fileName + UUID.randomUUID() + ".jpg"))) {
            ImageIO.write(image, "png", fos);
            System.out.println("pic is draw success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
